﻿
var type = getCookie('jiangjin').split("&")[3].split("=")[1];
if (type == '07332506fa00992798c91411c89023a8') {
    $("#exampleAccordion>li:nth-child(6) a").removeClass("collapsed");
    $("#exampleAccordion>li:nth-child(6) ul").addClass("show");
    $("#exampleAccordion>li:nth-child(6) ul>li:nth-child(1)").addClass("active");
}else{
    $("#exampleAccordion>li:nth-child(3) a").removeClass("collapsed");
    $("#exampleAccordion>li:nth-child(3) ul").addClass("show");
    $("#exampleAccordion>li:nth-child(3) ul>li:nth-child(1)").addClass("active");
}
var tool = 1;
function addPro() {
    if ($("#proMumber").val() == "") {
        layer.msg("产品数量不能为空")
        return false
    }
    var proName = $("#proName").find("option:selected")
    var proMoney = $("#proName")
    var proMumber = $("#proMumber")
    var ProNo = $("#proName").find("option:selected").data("prono");
    var html = '<tr><td>' + ProNo + '</td><td>' + proName.text() + '</td><td>' + proMoney.val() + '</td><td>' + proMumber.val() + '</td><td><a href="javascript:void(0);" onclick="DelPro(this)" class="text-danger">删除</a></td></tr>';;
    if (tool == 1) {
    	$(".Pro-table tbody").append(html);
        $("#panel").show({
            duration: 1500,
            easing: 'easeInOutElastic'
        });
        tool++;

    } else {
        $(".Pro-table tbody").append(html);
    }
    proMumber.val("");
}

$(function() {
    $(".Pro-table tr").css("text-align", "center");
    // 三级联动
    addressInit("province", "city", "district");

	$("#sellerNumber").blur(function () {
		$.ajax({
			type: "post",
			url: "/Order/GetSeller",
			data: { no: $("#sellerNumber").val() },
			success: function (result) {
				if (result.Code == "OK") {
					$("#tuijianren").text(result.Name);
				} else {
					$("#tuijianren").text("推荐人不存在");
				}
			}


		})
	});

})
//获取table里面所有的产品数据
function GetProData() {
    var Pro = [];
    $("#ProData").find("tbody").find("tr").each(function (index, Element) {
        var Product = {};
        Product.Name = $(this).find("td").eq(1).text()
        Product.Money = $(this).find("td").eq(2).text()
        Product.Num = $(this).find("td").eq(3).text()
        Product.No = $(this).find("td").eq(0).text()
        Pro.push(Product);
    })
    return Pro;
}
//删除所在行
function DelPro(t) {
    $(t).parents("tr").remove();
}
function submit() {
    var bootstrapValidator = $(".addOrder").data('bootstrapValidator');
    bootstrapValidator.validate();
    if (GetProData().length == 0) {
        layer.msg("请添加产品");
        return false
    }
    var Add = $("#province").val() + $("#city").val() + $("#district").val();
    if (bootstrapValidator.isValid()) {
        var option = {
            type: 'post',
			url: '/Order/Index',
            data: { SaleNo: $("#sellerNumber").val(), UserName: $("#purchaserName").val(), Mobile: $("#purchaserTel").val(), Add: Add, Address: $("#detailedSite").val(), Remarks: $("#Remarks").val(), Products: GetProData(), Wather: $("#Wather").val() },
            callback: SetMode
        }
        swGather.swAjax(option)
    }
}
//ajax回调
function SetMode(data) {
    layer.msg(data.Msg);
}

//验证
$(".addOrder").bootstrapValidator({
    message: '无效的值',
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh',
    },
    fields: {
        sellerNumber: {
            message: '无效的数值',
            validators: {
                notEmpty: {
                    message: '卖方编号，不能为空的'
                }
            }
        },
        purchaser: {
            message: '无效的数值',
            validators: {
                notEmpty: {
                    message: '买方昵称，不能为空的'
                }
            }
        },
        purchaserTel: {
            message: '无效的数值',
            validators: {
                notEmpty: {
                    message: '买方联系电话，不能为空的'
                },
                regexp: {
                    regexp: /^1[34578]\d{9}$/,
                    message: '请输入正确的手机号'
                }
            }
        },
        detailedSite: {
            message: '无效的数值',
            validators: {
                notEmpty: {
                    message: '买家详细地址，不能为空的'
                }
            }
        },
        Remarks: {
            message: '无效的数值',
            validators: {
                notEmpty: {
                    message: '备注，不能为空的'
                }
            }
        }

    }
})